package com.aurora.CETManagementSystem.controller;

import com.alibaba.fastjson.JSON;
import com.aurora.CETManagementSystem.mapper.AccountMapper;
import com.aurora.CETManagementSystem.mapper.TestLogMapper;
import com.aurora.CETManagementSystem.mapper.UserInfoMapper;
import com.aurora.CETManagementSystem.pojo.Account;
import com.aurora.CETManagementSystem.pojo.TestLog;
import com.aurora.CETManagementSystem.pojo.UserInfo;
import com.aurora.CETManagementSystem.utils.SomeUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.util.*;

@Controller
public class StudentController {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private AccountMapper accountMapper;

    @Resource
    private TestLogMapper testLogMapper;

    @Resource
    private SomeUtils someUtils;


    /**
     * 跳转到学生个人信息页面
     * @return
     */
    @RequestMapping("/toStudentInfo")
    public String toPatientList(Model model,HttpSession session){

        Account account = (Account) session.getAttribute("account");
        UserInfo student = userInfoMapper.selectById(account.getId());
        model.addAttribute("student",student);

        return "student/student_info";
    }

    /**
     * 跳转到考试报名页面
     * @return
     */
    @RequestMapping("/student/toSignUp")
    public String toSignUp(Model model){

        return "student/signup";
    }


    /**
     * 跳转到准考证打印页面
     * @return
     */
    @RequestMapping("/student/toTicket")
    public String toPrint(Model model,HttpSession session){

        Account account = (Account) session.getAttribute("account");
        UserInfo student = userInfoMapper.selectById(account.getId());
        List<TestLog> testLogs = testLogMapper.selectList(new QueryWrapper<TestLog>().eq("stunum", student.getStuNum()));
        if(testLogs.isEmpty()){
            model.addAttribute("msg","未查询到准考证信息，请先报名！");
            return "student/signup";
        }

        model.addAttribute("student",student);
        model.addAttribute("testLog",testLogs.get(0));


        return "student/ticket";
    }



    //个人信息修改功能
    @RequestMapping("/student/userInfo/update")
    @ResponseBody
    public String updateUserInfo(Integer info_id,String name, String id,Integer gender,String school,String grade,String major,String stu_class,String stu_num,HttpSession session){
        UserInfo userInfo = new UserInfo(info_id, name, id, gender, school, grade, major, stu_class, stu_num);
        System.out.println("test===>/student/userInfo/update----"+userInfo);
        int result = userInfoMapper.updateById(userInfo);

        if(stu_num.isEmpty()){
            return "学号不能为空！";
        }


        if(result==1){
            return "更新成功！";
        }else {
            return "更新失败！";
        }

    }



    //模拟报名四六级考试
    @RequestMapping("/student/signUp/print")
    @ResponseBody
    public String print(String school,String level,Model model,HttpSession session){

        System.out.println(school);
        System.out.println(level);


        Account account = (Account) session.getAttribute("account");
        UserInfo user = userInfoMapper.selectById(account.getId());
        System.out.println(user.toString());

        if(user.getName()==null||user.getStuNum().isEmpty()||user.getId().isEmpty()){
            return "请先完善个人信息！";
        }


        List<TestLog> testLogs = testLogMapper.selectList(new QueryWrapper<TestLog>().eq("stunum", user.getStuNum()).eq("cetlevel", level));
        if(testLogs.size()==0){

            TestLog testLog = new TestLog();
            testLog.setCetnum(someUtils.cetNumGenerate());
            testLog.setName(user.getName());
            testLog.setStunum(user.getStuNum());
            testLog.setStuid(user.getId());
            testLog.setCetlevel(level);
            testLog.setTestschool(school);

            int insert = testLogMapper.insert(testLog);
            if(insert==1){
                return "报名成功！";
            }

        }else {
            return "你已经报名过该次考试！";
        }


        return "报名失败！";
    }





    //学生修改密码功能
    @PostMapping("/student/account/updatePwd")
    @ResponseBody
    public String updatePwd(String oldPwd,String newPwd,String checkPwd,HttpSession session){
        System.out.println("oldPwd = " + oldPwd + ", newPwd = " + newPwd + ", checkPwd = " + checkPwd);
        if(oldPwd.isEmpty()){
            return "原密码不能为空!";
        }
        if(newPwd.isEmpty()){
            return "新密码不能为空!";
        }
        if(checkPwd.isEmpty()){
            return "确认密码不能为空!";
        }

        if(!newPwd.equals(checkPwd)){
            return "确认密码不一致!";
        }

        Account account = (Account) session.getAttribute("account");
        if(!oldPwd.equals(account.getPassword())){
            return "原密码不正确!";
        }

        account.setPassword(newPwd);
        int i = accountMapper.updateById(account);
        if(i==1){
            return "修改成功,请重新登录!";
        }else {
            return "修改失败!";
        }

    }


    @RequestMapping("/admin/delete/{id}")
    public String delete(@PathVariable("id") int id){
        userInfoMapper.deleteById(id);
        accountMapper.deleteById(id);
        return "redirect:/admin/toAccounts";
    }

    @RequestMapping("/admin/deletestu/{id}")
    public String deleteStu(@PathVariable("id") int id){
        userInfoMapper.deleteById(id);
        accountMapper.deleteById(id);
        return "redirect:/admin/toStudents";
    }

    /**
     * 根据姓名或者学号对学生进行查询
     * @return
     */
    @RequestMapping("/admin/student/search")
    public String studentSearch(String search,Model model){
        List<UserInfo> studentlist = userInfoMapper.selectList(new QueryWrapper<UserInfo>().like("name", search).or().like("stu_num", search));

        if(!studentlist.isEmpty()){
            model.addAttribute("studentlist", studentlist);
        }else {
            model.addAttribute("msg", "未查询到相关结果!");
        }

        return "admin/students";
    }


}
